<?php
session_start();
class Examiner extends CI_Controller{
    //put your code here
    
    public function __construct(){
        parent::__construct();
        $this->load->helper("url");
        $this->load->model('Login_m');
        $this->load->model('Account_m');
        $this->load->helper('form');
        $this->load->model('Download_m');
        $this->load->model('Test_model');
        $this->load->model('Comment_m');
        $this->load->model('Mark');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library("my_layout"); // Sử dụng thư viện layout
        $this->my_layout->setLayout("layout/examiner_l"); // load file layout chính (view/layout/frontend.php)
    
    }
    // Controller mặc định cho trang chủ
    public function index(){
        $data['title'] = "ホームページ";
        $data['data'] = "ホームページのデータ";
        $session_data_ex = $this->session->userdata('logged_in_ex');
	$data['id_ex'] = $session_data_ex['id_ex'];
        //$data['email_ex'] = $session_data_ex['email_ex'];
        if($session_data_ex['id_ex']!="")
        {
        $this->my_layout->view("frontend/home",$data);
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }

    public function forum(){
        $data['title'] = "フォーラム";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
	$data['id_ex'] = $session_data_ex['id_ex'];
        $this->my_layout->view("frontend/forum",$data);
        }
 else {
            redirect('login_sensei','refresh');
 }
    }
    public function about(){
        $data['title'] = "紹介";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
	$data['id_ex'] = $session_data_ex['id_ex'];
        $this->my_layout->view("frontend/about",$data);
        }
        else
            redirect ('login_sensei','refresh');
    }   
    public function exam(){
        $data['title'] = "採点";
        $data['data'] = "採点";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
	$data['id_ex'] = $session_data_ex['id_ex'];
        $this->my_layout->view("examiner/examiner_v",$data);
        }
        else
            redirect ('login_sensei','refresh');
    }
    function download()
    {
        $data['title']="ダウンロード";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
	$data['id_ex'] = $session_data_ex['id_ex'];
        $data['user'] = "examiner";
        $this->load->helper(array('form'));
        $this->my_layout->view('download/download_v',$data); 
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    function verifydownload()
    {
        $data['title']="ダウンロード";
        $session_data_ex = $this->session->userdata('logged_in_ex');
	$data['id_ex'] = $session_data_ex['id_ex'];
        $filedown=$this->input->post('filedown');
        $name=$this->Download_m->getCSVfromDB($filedown);
        if($session_data_ex['id_ex']!="")
        {
        $data['name']=$name;
        foreach ($name as $rows) {
        ////code hieu
            $session_array_download=array('name'=>$rows->name,'link'=>$rows->link);
            $this->session->set_userdata('download_file',$session_array_download);
            //echo $rows->Name;


           //echo anchor('downfile', 'Download');
            //anchor($uri, $title, $attributes)
            //echo "<br>";         
        }
        $this->my_layout->view('download/downfile_v',$data);
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    public function downfile($id)
    {
        //$session_download = $this->session->userdata('download_file');
        //$test=$session_download['name'];
        //$test=$_GET['STT'];
        $data = file_get_contents("./csv_source/".$id); 
        force_download($id, $data);
    }
    public function composemail()
    {
        $data['title']="メールの作成";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
	$data['id_ex'] = $session_data_ex['id_ex'];
        $data['email_ex']=$session_data_ex['email_ex'];
        $this->my_layout->view('comment/compose_mail_v',$data);
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    public function compose_mail_success()
    {
        $data['title']="メールを送ることが成功";
        $session_data_ex = $this->session->userdata('logged_in_ex');
	$data['id_ex'] = $session_data_ex['id_ex'];
        $email_ex=$session_data_ex['email_ex'];
        $email_to=$this->input->post('txtTo');
        $email_cc=$this->input->post('txtCc');
        $email_bcc=$this->input->post('txtBcc');
        $subject=$this->input->post('txtSubject');
        $content=$this->input->post('txtContent');
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 't06k53itjp@gmail.com';
        $config['smtp_pass']    = 'hedspit06';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'text'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not      
        $this->email->initialize($config);
        $this->email->from($email_ex);
        $this->email->subject($subject);
        /*$email_to_list=  explode(",", $email_to);
        for($i=0;$i<count($email_to_list);$i++)
        {*/
           $this->email->to($email_to); 
        //}
        /*$email_cc_list=  explode(",", $email_cc);
        for($j=0;$j<count($email_cc_list);$j++)
        {*/
           $this->email->cc($email_cc); 
           //echo $email_cc_list[$j];
        //}
        
        /*$email_bcc_list=  explode(",", $email_bcc);
        for($k=0;$k<count($email_bcc_list);$k++)
        {*/
           $this->email->bcc($email_bcc); 
       // }
        $this->email->message($content);
        if($this->email->send())
        {
            $this->Comment_m->insert_mail($email_ex,$email_to,$email_cc,$email_bcc,$subject,$content);
            redirect('examiner/sendmail');
            
        }
        else
            echo "Error send mail";
        
    }
    public function sendmail()
    {
        $data['title']="送ったメール";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
        $data['id_ex'] = $session_data_ex['id_ex'];
        $email_ex=$session_data_ex['email_ex'];
        $data['sendmail']=$this->Comment_m->list_send_mail($email_ex);
        $this->my_layout->view('comment/list_send_mail_v',$data);
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    public function inbox()
    {
        $data['title']="インボックス";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
        $data['id_ex'] = $session_data_ex['id_ex'];
        $email_ex=$session_data_ex['email_ex'];
        $data['inboxmail']=$this->Comment_m->list_inbox_mail($email_ex);
        $this->my_layout->view('comment/list_inbox_v',$data);
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    public function replyMail($id_com)
    {
        $data['title']="メールの返事";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        $data['id_ex'] = $session_data_ex['id_ex'];
        $data['email_ex']=$session_data_ex['email_ex'];
        $query=$this->Comment_m->get_comment($id_com);
        if($session_data_ex['id_ex']!="")
        {
        foreach ($query->result() as $row) {
            
            $data['email_from']=$row->email_from;
            $data['email_to']=$row->email_to;
            $data['subject']=$row->subject;
            $data['content']=$row->content;
        }
        $this->my_layout->view('comment/reply_mail_v',$data);
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    public function reply_mail_success()
    {
        $data['title']="メールを送ることが成功";
        $session_data_ex = $this->session->userdata('logged_in_ex');
	$data['id_ex'] = $session_data_ex['id_ex'];
        $email_ex=$session_data_ex['email_ex'];
        $email_to=$this->input->post('txtTo');
        $subject=$this->input->post('txtSubject');
        $content=$this->input->post('txtContent');
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 't06k53itjp@gmail.com';
        $config['smtp_pass']    = 'hedspit06';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'text'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not      
        $this->email->initialize($config);
        $this->email->to($email_to);
        $this->email->from($email_ex);
        $this->email->subject($subject);
        $this->email->message($content);
        if($this->email->send())
        {
            $this->Comment_m->reply_mail($email_ex,$email_to,$subject,$content);
            redirect('examiner/inbox');
            
        }
        else
            echo "Error send mail";
    }
    public function deleteInboxMail($id_com)
    {
        $data['title']="メールの削除";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
	$data['id_ex'] = $session_data_ex['id_ex'];
        $this->Comment_m->deleteMail($id_com);
        redirect('examiner/inbox');
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    public function deleteSendMail($id_com)
    {
        $data['title']="メールの削除";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
	$data['id_ex'] = $session_data_ex['id_ex'];
        $this->Comment_m->deleteMail($id_com);
        redirect('examiner/sendmail');
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    public function mark()
    {
        $data['title']="採点";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
        $data['id_ex'] = $session_data_ex['id_ex'];
        $query=$this->Mark->get_idtester();
        $data['id_te_sub']=$query->result();
        $this->my_layout->view('mark/form_mark_v',$data);
        }
        else {
            redirect('login_sensei','refresh');
        }
    }
    public function view_test_sub()
    {
        $data['title']="採点";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        if($session_data_ex['id_ex']!="")
        {
        $data['id_ex'] = $session_data_ex['id_ex'];
        $id_te_sub=$this->input->post('txtIDTest');
        $data['id_te_sub']=$id_te_sub;
        $query=$this->Mark->get_ans_byid($id_te_sub);
        $data['list_ques_id']=$query->result();
        $this->my_layout->view('mark/view_test_sub_v',$data);
        }
        else
        {
            redirect('login_sensei','refresh');
        }
    }
    public function mark_mail()
    {
        $query10=$this->Test_model->select_tester();
        $data['title']="採点を送る";
        $session_data_ex = $this->session->userdata('logged_in_ex');
        $data['id_ex'] = $session_data_ex['id_ex'];
        $email_from_mark=$session_data_ex['email_ex'];
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 't06k53itjp@gmail.com';
        $config['smtp_pass']    = 'hedspit06';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'text'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not      
        $this->email->initialize($config);
        $Subject="点の情報";        
        $this->email->from($email_from_mark);
        $this->email->subject($Subject);
        foreach ($query10->result() as $rows)
        {
            $this->email->to($rows->email);
            $Content="あなたの点は".$rows->sum_mark;
            $this->email->set_newline("\r\n");
            $this->email->message($Content);
            if($this->email->send())
            {
                redirect('examiner/mark');
            }
            else
                echo "Send mail error";
        }
    }
    
    
    //examiner＿profile
    public function examiner_profile()
    {
         $data['title'] = "プロファイル";
         $session_data_sm = $this->session->userdata('logged_in_ex');
         if($session_data_sm['id_ex']!="")
         {
	 $data['id_ex'] = $session_data_sm['id_ex'];
         $query=$this->Account_m->get_examiner($data['id_ex']);
         foreach ($query as $row)
         {
            $data['id_mg']=$row->id_ex;
            $data['email_mg']=$row->email;
            $data['name_mg']=$row->name;
            $data['phone_mg']=$row->phone;
            $data['address_mg']=$row->address;
         }
         $this->my_layout->view("frontend/profile_v",$data);
         }
         else
         {
             redirect('login_sensei','refresh');
         }
         
    }
    //manager_change pass
    public function examiner_change_pass()
    {
        $data['title'] = "パスワードの修正";
        $data['user']="examiner";
        $session_data_sm = $this->session->userdata('logged_in_ex');
	$data['id_ex'] = $session_data_sm['id_ex'];
        $this->my_layout->view("frontend/change_pass",$data);
        
    }
    //manager_change pass
    public function success_change_pass()
    {
        $data['title'] = "パスワードの修正";
        $data['user']="examiner";
        $session_data_sm = $this->session->userdata('logged_in_ex');
	$data['id_ex'] = $session_data_sm['id_ex'];
        $new_pass1=$this->input->post('new_pass');
        $new_pass2=$this->input->post('new_pass_again');
        $old_pass=$this->input->post('old_pass');
        $query= $this->Account_m->search_examiner_pass($data['id_ex']);
        foreach ($query as $row)
         {
            $data['password']=$row->password;        
         }
        if($data['password']!=$old_pass)
         {
             $data['error1']="古いパスワードが違う";
             $this->my_layout->view("frontend/change_pass",$data);
         }
        else if($new_pass1!=$new_pass2)
        {
        $data['error']="新しいパスワードと新しいパスワードの重複が違う";
        $this->my_layout->view("frontend/change_pass",$data);
        }
        else
        {
            $this->Account_m->update_examiner_pass($data['id_ex'],$new_pass1);
             echo "<script type='text/javascript'>";	
	    echo "alert ('パスワードを変更しました!!! ');";
	    echo "</script>";
             $this->my_layout->view("frontend/home",$data);
        }
    }
    
    public function logout()
    {
        $session_data_ex = $this->session->userdata('logged_in_ex');
        $id_ex = $session_data_ex['id_ex'];
        $manager_ex = $session_data_ex['manager_ex'];
        $this->Login_m->update_online_ex1($id_ex,$manager_ex,0);
        $this->session->unset_userdata('logged_in_ex');
        session_destroy();
        redirect('login_sensei','refresh');
    } 
    
}
?>
